package com.ce.cetag.tags.ajaxselect;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import com.ce.cetag.model.AjaxSelectModel;
import com.ce.cetag.util.BlankUtil;

/**
 *
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 * <p>功能描述：Ajax联动下拉框标签
 *
 * @author kuanglingxuan
 * Created on 2007-8-21
 */
public class UnitedSelectTag extends TagSupport {

	public int doStartTag() throws JspTagException{
//		List dataList = (List)pageContext.getRequest().getAttribute(this.getId());//取数据构造本下拉框
//		System.out.println("dataList--->"+dataList);

		if(this.getUrl() != null && this.getActionName() != null){

			throw new JspTagException(((HttpServletRequest)pageContext.getRequest())
					.getServletPath()+", According to the author, must add only one attribute of actionName and url.");
		}

		if(this.getUrl() == null)
			this.setUrl(getFormatUrl());
		StringBuffer out = new StringBuffer();
		out.append("<select ");
		if(id != null && !"".equals(id)){
			out.append(" id=\""+this.getId()+"\" ");
		}
		out.append(" name=\""+this.getName()+"\" ");
		if(this.getClassName() != null && !"".equals(this.getClassName())){
			out.append(" class=\""+this.getClassName()+"\"");
		}

		if(!BlankUtil.isBlank(this.getStyle())){
			out.append(" style=\""+this.getStyle()+"\" ");
		}

		if(this.getIsUnited() == true){

			if(this.getTargetId() == null || this.getId() == null){
				throw new JspTagException(((HttpServletRequest)pageContext.getRequest())
						.getServletPath()+", if isUnited attribute is true, the targetId and id attribute are required.");
			}

			out.append(" onChange=\"");
			if(!BlankUtil.isBlank(this.getBeforeOnChange())){
				if(this.getBeforeOnChange().length() == (this.getBeforeOnChange().indexOf(";")+1)) {
					out.append(this.getBeforeOnChange());
				} else {
					out.append(this.getBeforeOnChange()+";");
				}

			}

			if(!BlankUtil.isBlank(this.getOnChangeCallBack())){
				out.append("onChangeSelect('"+this.getUrl()+"',this.value,'"+this.getTargetId()+"','"+this.getOnChangeCallBack()+"');");
			} else {
				out.append("onChangeSelect('"+this.getUrl()+"',this.value,'"+this.getTargetId()+"','');");
			}


			if(!BlankUtil.isBlank(this.getAfterOnChange())){
				if(this.getAfterOnChange().length() == (this.getAfterOnChange().indexOf(";")+1)) {
					out.append(this.getAfterOnChange());
				} else {
					out.append(this.getAfterOnChange()+";");
				}
			}
			out.append("\"");

		}else{

			if(!BlankUtil.isBlank(this.getOnChange()))
				out.append(" onChange=\""+this.getOnChange()+"\"");
		}

		if(!BlankUtil.isBlank(this.multiple)) {
			out.append(" multiple=\"multiple\"");
		}

		if(!BlankUtil.isBlank(this.size)) {
			out.append(" size=\""+this.getSize()+"\"");
		}

		if(!BlankUtil.isBlank(this.ondblclick)) {
			out.append(" ondblclick=\""+this.getOndblclick()+"\"");
		}

		if(!BlankUtil.isBlank(this.tabIndex)) {
			out.append(" tabIndex=\""+this.getTabIndex()+"\"");
		}

		out.append(" >");
		if(!BlankUtil.isBlank(this.getSelectText())){
			out.append(" <option value=\"\">");
			out.append(this.getSelectText());
			out.append("</option> ");
		}
		if(dataList != null && dataList.size() > 0){
			for(int i=0;i<dataList.size();i++){
				if(dataList.get(i) instanceof AjaxSelectModel){
					AjaxSelectModel node = (AjaxSelectModel)dataList.get(i);
					String initValue = getValue();
					if(initValue != null && initValue.equals(node.getId())){
						out.append(" <option selected  id=\""+this.id +"_"+node.getId()+"\" value=\""+node.getId()+"\">"+ node.getText() +"</option> ");
					}else{
						out.append(" <option value=\""+node.getId()+"\">"+ node.getText() +"</option> ");
					}
				}else if(optionValue != null && !"".equals(optionValue)){
					String initValue = getValue();
//					System.out.println("initValue--->"+initValue);
					Class clazz = dataList.get(i).getClass();
					try {
						if(optionValue.length() < 2 || optionLabel.length() < 2)
							throw new JspTagException(((HttpServletRequest)pageContext.getRequest())
									.getServletPath()+", the length optionValue or optionLabel attribute must both larger than 2 .");

						Boolean isUpperValue = Character.isUpperCase(optionValue.substring(1,2).charAt(0));
						Boolean isUpperLabel = Character.isUpperCase(optionLabel.substring(1,2).charAt(0));
						String valueMethodName = "get"+(isUpperValue == true?optionValue.substring(0,1):optionValue.substring(0,1).toUpperCase())+optionValue.substring(1);
						String labelMethodName = "get"+(isUpperLabel == true?optionLabel.substring(0,1):optionLabel.substring(0,1).toUpperCase())+optionLabel.substring(1);
						Method valueMethod = clazz.getMethod(valueMethodName, null);
						Method labelMethod = clazz.getMethod(labelMethodName, null);
						String value = valueMethod.invoke(dataList.get(i), null).toString();
						String label = labelMethod.invoke(dataList.get(i), null).toString();
//						System.out.println("label--->"+label);

						if(initValue != null && initValue.equals(value)){
//							System.out.println("value1--->"+value);
							out.append(" <option selected id=\""+this.id +"_"+value+"\"  value=\""+value+"\">"+ label +"</option> ");
						}else{
//							System.out.println("value2--->"+value);
							out.append(" <option id=\""+this.id +"_"+value+"\" value=\""+value+"\">"+ label +"</option> ");
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				} else{

					String initValue = getValue();
					String value = dataList.get(i).toString();
					if(initValue != null && initValue.equals(value)){

						out.append(" <option selected id=\""+this.id +"_"+dataList.get(i)+"\"  value=\""+value+"\">"+ value +"</option> ");
					}else{

						out.append(" <option id=\""+this.id +"_"+value+"\" value=\""+value+"\">"+ value +"</option> ");
					}

					//out.append(" <option value=\""+dataList.get(i)+"\">"+ dataList.get(i) +"</option> ");
				}

			}
		}
		out.append("</select>");


		try {


			pageContext.getOut().write(out.toString());

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return SKIP_BODY;


	  }


	public int doEndTag() throws JspTagException{

		this.setUrl(null);

		return EVAL_PAGE;



	}

	/**
	 * 功能描述：因为集成struts，所以给出actionName属性输入，按照规定url格式进行组装。
	 *         如果用servlet可以不使用actionName而在页面自己按规定url格式组装。
	 * @return　url
	 */
	private String getFormatUrl(){
		String str = ((HttpServletRequest)pageContext.getRequest()).getContextPath()
			+ "/" + this.getActionName() + ".do?method=unitedSelect&selectId='+this.value+'&targetId="
			+ this.getTargetId();
		return str;

	}

	private String url;//页面输入的url，使用servlet时可输入
	private String targetId;//下一级下拉框的id
	private String id;//本下拉框id
	private String name;//本下拉框name
	private String actionName;//struts的actionName
	private String value;//初始化下拉框定位value
	private String selectText;//请选择的国际化文本
	private List dataList;//下拉框数据集合
	private String className;//下拉框样式名
	private String style;
	private Boolean isUnited = false;//假单个，真联动
	private String optionLabel;//取label方法名
	private String optionValue;//取value方法名
	private String onChange;   //只针对单个下来框的，不对联动的起作用
	private String beforeOnChange;//只针对联动的，对单个下拉框的不起作用
	private String afterOnChange;//只针对联动的，对单个下拉框的不起作用
	private String onChangeCallBack;////回调函数，只针对联动的，对单个下拉框的不起作用
	private String multiple;    //多项选择
	private Integer size;       //可视多少行
	private String ondblclick;
	private Integer tabIndex;   //Tab 的位置


	public Integer getTabIndex() {
		return tabIndex;
	}


	public void setTabIndex(Integer tabIndex) {
		this.tabIndex = tabIndex;
	}


	public String getAfterOnChange() {
		return afterOnChange;
	}


	public void setAfterOnChange(String afterOnChange) {
		this.afterOnChange = afterOnChange;
	}


	public String getBeforeOnChange() {
		return beforeOnChange;
	}


	public void setBeforeOnChange(String beforeOnChange) {
		this.beforeOnChange = beforeOnChange;
	}


	public String getOnChange() {
		return onChange;
	}


	public void setOnChange(String onChange) {
		this.onChange = onChange;
	}


	public String getOptionLabel() {
		return optionLabel;
	}


	public void setOptionLabel(String optionLabel) {
		this.optionLabel = optionLabel;
	}


	public String getOptionValue() {
		return optionValue;
	}


	public void setOptionValue(String optionValue) {
		this.optionValue = optionValue;
	}


	public Boolean getIsUnited() {
		return isUnited;
	}


	public void setIsUnited(Boolean isUnited) {
		this.isUnited = isUnited;
	}


	public String getClassName() {
		return className;
	}


	public void setClassName(String className) {
		this.className = className;
	}

	public String getStyle() {
		return style;
	}


	public void setStyle(String style) {
		this.style = style;
	}


	public String getValue() {
		return value;
	}


	public void setValue(String value) {
		this.value = value;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public String getTargetId() {
		return targetId;
	}


	public void setTargetId(String targetId) {
		this.targetId = targetId;
	}


	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	}


	public String getActionName() {
		return actionName;
	}


	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public List getDataList() {
		return dataList;
	}


	public void setDataList(List dataList) {
		this.dataList = dataList;
	}


	public String getSelectText() {
		return selectText;
	}


	public void setSelectText(String selectText) {
		this.selectText = selectText;
	}
	public String getOnChangeCallBack() {
		return onChangeCallBack;
	}


	public void setOnChangeCallBack(String onChangeCallBack) {
		this.onChangeCallBack = onChangeCallBack;
	}

	public String getMultiple() {
		return multiple;
	}


	public void setMultiple(String multiple) {
		this.multiple = multiple;
	}


	public Integer getSize() {
		return size;
	}


	public void setSize(Integer size) {
		this.size = size;
	}

	public String getOndblclick() {
		return ondblclick;
	}


	public void setOndblclick(String ondblclick) {
		this.ondblclick = ondblclick;
	}

	public void release(){
//		System.out.println("in release()");
		super.release();
		this.url = null;
		this.targetId = null;
		this.id = null;
		this.name = null;
		this.actionName = null;
		this.className = null;
		this.style = null;
		this.value = null;
		this.selectText = null;
		this.dataList = null;
		this.beforeOnChange = null;
		this.afterOnChange = null;
		this.isUnited = null;
		this.onChangeCallBack = null;
		this.multiple = null;
		this.size = null;
		this.ondblclick = null;
		this.tabIndex = null;
	}







}
